﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Helpers;
using System.Web;
using System.Web.WebPages;
using System.IO;
using System.Web.Mvc;
using System.Diagnostics;

namespace SRF.UI.Html
{
    public static class ListPageExtensions
    {
        /// <summary>
        /// 分页信息模板
        /// </summary>
        static readonly string pageInfoFormat = "{curpageno}/{pagecount},共{rowcount}条记录本页{curpagesize}条";
        static readonly string morePage = "<li class='disabled'><a>...</a></li>";

        /// <summary>
        /// 分页条
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="pager">页面信息</param>
        /// <param name="showPageInfo">是否显示分页信息</param>
        /// <returns></returns>
        public static MvcHtmlString PageBar(this HtmlHelper htmlHelper, SRF.UI.IPager pager, bool showPageInfo = true)
        {
            int pageNoCount = 5;
            int startPageNo;
            string html, prevHtml, nextHtml, pageInfoHtml, paginationHtml, url;
            pageInfoHtml = "";
            UrlBuilder urlBuilder = new UrlBuilder(htmlHelper.ViewContext.HttpContext.Request);
            StringBuilder pageNoHtml = new StringBuilder();

            //页码格式
            urlBuilder["page"] = "{0}";
            url = urlBuilder.ToString();

            startPageNo = (pager.CurPageNo > (pageNoCount / 2 + pageNoCount % 2)) ? (pager.CurPageNo - pageNoCount / 2) : 1;

            for (int i = startPageNo; i < startPageNo + pageNoCount && i <= pager.PageCount; i++) //页码
            {
                pageNoHtml.AppendFormat("<li class='{0}'><a href='{1}'>{2}</a><li>", i == pager.CurPageNo ? "active" : "", string.Format(url, i), i);
            }

            if (startPageNo > 1)  //第一页
            { 
                pageNoHtml.Insert(0, string.Format("<li><a href='{0}'>1</a></li>{1}", string.Format(url, 1), startPageNo > 2 ? morePage : ""));
            }
            if (startPageNo + pageNoCount <= pager.PageCount) //最后一页
            {
                pageNoHtml.AppendFormat("{0}<li><a href='{1}' title='最后一页'>{2}</a></li>", (startPageNo + pageNoCount) < pager.PageCount ? morePage : "", string.Format(url, pager.PageCount), pager.PageCount);
            }
            //上一页
            if (pager.CurPageNo <= 1)
            {
                prevHtml = "<li class='disabled'><a>&laquo;</a></li>";
            }
            else
            {
                prevHtml = string.Format("<li><a href='{0}' title='上一页'>&laquo;</a><li>", string.Format(url, pager.CurPageNo - 1));
            }
            //下一页
            if (pager.CurPageNo == pager.PageCount)
            {
                nextHtml = "<li class='disabled'><a>&raquo;</a></li>";
            }
            else
            {
                nextHtml = string.Format("<li><a href='{0}' title='下一页'>&raquo;</a></li>", string.Format(url, pager.CurPageNo + 1));
            }
            paginationHtml = string.Format("<div class='pagination'><ul>{0}{1}{2}<ul></div>", prevHtml, pageNoHtml, nextHtml);

            //分页信息
            if (showPageInfo == true)
            {
                pageInfoHtml = pageInfoFormat;
                pageInfoHtml = pageInfoHtml.Replace("{rowcount}", pager.RowCount.ToString());
                pageInfoHtml = pageInfoHtml.Replace("{pagecount}", pager.PageCount.ToString());
                pageInfoHtml = pageInfoHtml.Replace("{curpageno}", pager.CurPageNo.ToString());
                pageInfoHtml = pageInfoHtml.Replace("{curpagesize}", pager.CurPageSize.ToString());
                pageInfoHtml = "<span class='info'>" + pageInfoHtml + "</span>";
            }

            html = string.Format("<div class='row pagebar'>{0}{1}</div>", paginationHtml, pageInfoHtml);

            return MvcHtmlString.Create(html);
        }
    }
}